package cc.vv.btpartyjob.jobactivities.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.baselibrary.view.LKCircleImageView
import cc.vv.btpartyjob.jobactivities.R
import cc.vv.btpartyjob.jobactivities.inter.JobItemClickInterface
import cc.vv.btpartyjob.jobactivities.moudel.bean.JobBean
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import java.util.*

/**
 * 展示数据的自定义view
 * Created by  Chen.qi on 2018/10/24.
 */
class JobOperationView : LinearLayout {
    /**
     * 上下文
     */
    private var mContext: Context? = null
    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_job_operation_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_job_showAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    fun addAllSelectedFileView(optionList: ArrayList<JobBean>?, itemSize: Int, itemClickCallBack: JobItemClickInterface?) {
        if (null == optionList) {
            return
        }
        //清除
        clearAllJobOptionView()
        val length = optionList.size
        val sum = length / itemSize
        val add = if (length % itemSize == 0) 0 else 1
        val totalLinLayout = sum + add

        val viewWight = (LKScreenUtil.getScreenWidth()) / itemSize
        val layoutParams1 = LayoutParams(viewWight, LayoutParams.WRAP_CONTENT)

        for (j in 0 until totalLinLayout) {
            val honLinearLayout = LinearLayout(context)
            if (j > 0) {
                honLinearLayout.setPadding(0, LKScreenUtil.dp2px(10f), 0, 0)
            }
            val iNumber = j * itemSize
            var count = 0
            for (i in iNumber until optionList.size) {
                count++
                if (count == itemSize + 1) {
                    break
                }
                val view = LayoutInflater.from(mContext).inflate(R.layout.layout_job_item, null)
                view.layoutParams = layoutParams1
                val img = view.findViewById<ImageView>(R.id.iv_job_itemPic)
                val descText = view.findViewById<TextView>(R.id.tv_job_itemStr)
                val layoutParams = img.layoutParams as LinearLayout.LayoutParams
                layoutParams.width = LKScreenUtil.dp2px(56f)
                layoutParams.height = LKScreenUtil.dp2px(56f)
                img.layoutParams = layoutParams
                val jobOptionObj = optionList[i]
                view.setOnClickListener {
                    itemClickCallBack?.clickItem(jobOptionObj)
                }
                setPic(jobOptionObj.title, img)
                descText.text = jobOptionObj.title
                honLinearLayout.addView(view)
            }
            llSelectedAll?.addView(honLinearLayout)
        }
    }

    /**
     * 清除view
     */

    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }

    private fun setPic(title: String?, img: ImageView?) {
        when (title) {
            "宝塔头条" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_bttt).into(img)
            }
            "街道头条" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_jdtt).into(img)
            }
            "社区头条" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_sqtt).into(img)
            }
            "网格头条" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_wgtt).into(img)
            }
            "农村党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_ncdzz).into(img)
            }
            "机关党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_jgdzz).into(img)
            }
            "社区党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_sqdzz).into(img)
            }
            "学校党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_xxdzz).into(img)
            }
            "非公党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_fgdzz).into(img)
            }
            "社会组织党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_shzzdzz).into(img)
            }
            "国家企事业单位党组织" -> {
                LKImage.load().scale(ScaleMode.CENTER_CROP).loadRes(R.mipmap.icon_job_gjqsy).into(img)
            }
        }
    }


}